<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 类别管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" align="center" width="80"></el-table-column>
      <el-table-column label="图标" align="center" width="80">
        <template slot-scope="scope">
          <el-avatar shape="square" size="small" :src="scope.row.icon"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center" width="120"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center" width="100"></el-table-column>
      <el-table-column prop="enable" label="是否启用" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#aaa">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="isDisplay" label="是否显示" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#aaa">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="查看子级" align="center" width="100">
        <template slot-scope="scope">
          <el-button size="mini" :disabled="scope.row.isParent == 0"
                     @click="showSubCategoryList(scope.row)">查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" circle></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" circle
                     :disabled="scope.row.isParent == 1"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0 0 0; height: 30px;">
      <el-button size="mini" v-if="history.length > 0"
                 @click="goBack()">返回
      </el-button>

      <el-pagination style="float: right;"
                     @current-change="changePage"
                     hide-on-single-page
                     layout="total, prev, pager, next"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      // parentId: 0
      history: [],
      total: 0,
      pageSize: 0,
      currentPage: 0
    }
  },
  methods: {
    changePage(page) {
      let parentId = this.$router.currentRoute.query.parentId;
      if (!parentId) {
        parentId = 0;
      }

      this.$router.replace('?parentId=' + parentId + '&page=' + page);
      this.loadCategoryList();
    },
    goBack() {
      let parentCategory = this.history.pop();

      this.$router.replace('?parentId=' + parentCategory.parentId);
      this.loadCategoryList();
    },
    showSubCategoryList(category) {
      // this.parentId = category.id;

      this.history[category.depth - 1] = category;
      localStorage.setItem('mall:category:history', JSON.stringify(this.history));
      console.log('----------');
      for (let i = 0; i < this.history.length; i++) {
        console.log('id=' + this.history[i].id
            + ', name=' + this.history[i].name
            + ', parentId=' + this.history[i].parentId);
      }
      console.log('----------');

      this.$router.replace('?parentId=' + category.id)
      this.loadCategoryList();
    },
    openDeleteConfirm(category) {
      let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    handleDelete(category) {
      let url = 'http://localhost:29182/categories/' + category.id + '/delete';
      console.log('url = ' + url);

      this.axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadCategoryList();
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    loadCategoryList() {
      let parentId = this.$router.currentRoute.query.parentId;
      if (!parentId) {
        parentId = 0;
      }

      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:29182/categories/list-by-parent?parentId='
          + parentId + '&page=' + page;
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;

          if (parentId == 0) {
            this.history = [];
          }
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
  },
  mounted() {
    this.loadCategoryList();

    let categoryHistoryString = localStorage.getItem('mall:category:history');
    if (categoryHistoryString) {
      this.history = JSON.parse(categoryHistoryString);
    }
  }
}
</script>

<style scoped>

</style>